using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class QuestionDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procQuestion_add]";
        private string _strSPUpdateName = "dbo.[procQuestion_update]";
        private string _strSPDeleteName = "dbo.[procQuestion_delete]";
        private string _strSPGetName = "dbo.[procQuestion_get]";
        private string _strSPGetName_TrueAnswer = "dbo.[procQuestion_get_TrueAnswer]";
        private string _strSPGetAllName = "dbo.[procQuestion_getall]";
        private string _strSPGetPages = "dbo.[procQuestion_getpaged]";
        private string _strSPIsExist = "dbo.[procQuestion_isexist]";
        private string _strTableName = "[Question]";
        private string _strSPGetTransferOutName = "dbo.[procQuestion_gettransferout]";
        string _strSPGetEnableName = "procQuestion_getenable";
        #endregion Local Variable

        #region Method
        public QuestionInfo Get(
        Int64 ID,
        ref string sErr)
        {
            QuestionInfo objEntr = new QuestionInfo();
            connect();
            InitSPCommand(_strSPGetName);
            AddParameter(QuestionInfo.Field.ID.ToString(), ID);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();

            if (list.Rows.Count > 0)
                objEntr = (QuestionInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            QuestionInfo result = new QuestionInfo();
            result.ID = (dt.Rows[i][QuestionInfo.Field.ID.ToString()] == DBNull.Value ? 0 : Convert.ToInt64(dt.Rows[i][QuestionInfo.Field.ID.ToString()]));
            result.Description = (dt.Rows[i][QuestionInfo.Field.Description.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.Description.ToString()]));
            result.Explanation = (dt.Rows[i][QuestionInfo.Field.Explanation.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.Explanation.ToString()]));
            result.Type = (dt.Rows[i][QuestionInfo.Field.Type.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.Type.ToString()]));
            result.Difficulty = (dt.Rows[i][QuestionInfo.Field.Difficulty.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][QuestionInfo.Field.Difficulty.ToString()]));
            result.Rate = (dt.Rows[i][QuestionInfo.Field.Rate.ToString()] == DBNull.Value ? 0 : Convert.ToDouble(dt.Rows[i][QuestionInfo.Field.Rate.ToString()]));
            result.Timer = (dt.Rows[i][QuestionInfo.Field.Timer.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][QuestionInfo.Field.Timer.ToString()]));
            result.Picture = (dt.Rows[i][QuestionInfo.Field.Picture.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.Picture.ToString()]));
            result.Sound = (dt.Rows[i][QuestionInfo.Field.Sound.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.Sound.ToString()]));
            result.Enabled = (dt.Rows[i][QuestionInfo.Field.Enabled.ToString()] == DBNull.Value ? true : Convert.ToBoolean(dt.Rows[i][QuestionInfo.Field.Enabled.ToString()]));
            result.ANAL_Q0 = (dt.Rows[i][QuestionInfo.Field.ANAL_Q0.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.ANAL_Q0.ToString()]));
            result.ANAL_Q1 = (dt.Rows[i][QuestionInfo.Field.ANAL_Q1.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.ANAL_Q1.ToString()]));
            result.ANAL_Q2 = (dt.Rows[i][QuestionInfo.Field.ANAL_Q2.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.ANAL_Q2.ToString()]));
            result.ANAL_Q3 = (dt.Rows[i][QuestionInfo.Field.ANAL_Q3.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.ANAL_Q3.ToString()]));
            result.ANAL_Q4 = (dt.Rows[i][QuestionInfo.Field.ANAL_Q4.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.ANAL_Q4.ToString()]));
            result.ANAL_Q5 = (dt.Rows[i][QuestionInfo.Field.ANAL_Q5.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.ANAL_Q5.ToString()]));
            result.ANAL_Q6 = (dt.Rows[i][QuestionInfo.Field.ANAL_Q6.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.ANAL_Q6.ToString()]));
            result.ANAL_Q7 = (dt.Rows[i][QuestionInfo.Field.ANAL_Q7.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.ANAL_Q7.ToString()]));
            result.ANAL_Q8 = (dt.Rows[i][QuestionInfo.Field.ANAL_Q8.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.ANAL_Q8.ToString()]));
            result.ANAL_Q9 = (dt.Rows[i][QuestionInfo.Field.ANAL_Q9.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][QuestionInfo.Field.ANAL_Q9.ToString()]));

            return result;
        }

        public DataTable GetAll(
        ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetAllName);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(QuestionInfo objEntr, ref string sErr)
        {
            int ret = -1;
            connect();
            InitSPCommand(_strSPInsertName);
            AddParameter(QuestionInfo.Field.Description.ToString(), objEntr.Description);
            AddParameter(QuestionInfo.Field.Explanation.ToString(), objEntr.Explanation);
            AddParameter(QuestionInfo.Field.Type.ToString(), objEntr.Type);
            AddParameter(QuestionInfo.Field.Difficulty.ToString(), objEntr.Difficulty);
            AddParameter(QuestionInfo.Field.Rate.ToString(), objEntr.Rate);
            AddParameter(QuestionInfo.Field.Timer.ToString(), objEntr.Timer);
            AddParameter(QuestionInfo.Field.Picture.ToString(), objEntr.Picture);
            AddParameter(QuestionInfo.Field.Sound.ToString(), objEntr.Sound);
            AddParameter(QuestionInfo.Field.Enabled.ToString(), objEntr.Enabled);
            AddParameter(QuestionInfo.Field.ANAL_Q0.ToString(), objEntr.ANAL_Q0);
            AddParameter(QuestionInfo.Field.ANAL_Q1.ToString(), objEntr.ANAL_Q1);
            AddParameter(QuestionInfo.Field.ANAL_Q2.ToString(), objEntr.ANAL_Q2);
            AddParameter(QuestionInfo.Field.ANAL_Q3.ToString(), objEntr.ANAL_Q3);
            AddParameter(QuestionInfo.Field.ANAL_Q4.ToString(), objEntr.ANAL_Q4);
            AddParameter(QuestionInfo.Field.ANAL_Q5.ToString(), objEntr.ANAL_Q5);
            AddParameter(QuestionInfo.Field.ANAL_Q6.ToString(), objEntr.ANAL_Q6);
            AddParameter(QuestionInfo.Field.ANAL_Q7.ToString(), objEntr.ANAL_Q7);
            AddParameter(QuestionInfo.Field.ANAL_Q8.ToString(), objEntr.ANAL_Q8);
            AddParameter(QuestionInfo.Field.ANAL_Q9.ToString(), objEntr.ANAL_Q9);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeSPScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(QuestionInfo objEntr)
        {
            connect();
            InitSPCommand(_strSPUpdateName);
            AddParameter(QuestionInfo.Field.ID.ToString(), objEntr.ID);
            AddParameter(QuestionInfo.Field.Description.ToString(), objEntr.Description);
            AddParameter(QuestionInfo.Field.Explanation.ToString(), objEntr.Explanation);
            AddParameter(QuestionInfo.Field.Type.ToString(), objEntr.Type);
            AddParameter(QuestionInfo.Field.Difficulty.ToString(), objEntr.Difficulty);
            AddParameter(QuestionInfo.Field.Rate.ToString(), objEntr.Rate);
            AddParameter(QuestionInfo.Field.Timer.ToString(), objEntr.Timer);
            AddParameter(QuestionInfo.Field.Picture.ToString(), objEntr.Picture);
            AddParameter(QuestionInfo.Field.Sound.ToString(), objEntr.Sound);
            AddParameter(QuestionInfo.Field.Enabled.ToString(), objEntr.Enabled);
            AddParameter(QuestionInfo.Field.ANAL_Q0.ToString(), objEntr.ANAL_Q0);
            AddParameter(QuestionInfo.Field.ANAL_Q1.ToString(), objEntr.ANAL_Q1);
            AddParameter(QuestionInfo.Field.ANAL_Q2.ToString(), objEntr.ANAL_Q2);
            AddParameter(QuestionInfo.Field.ANAL_Q3.ToString(), objEntr.ANAL_Q3);
            AddParameter(QuestionInfo.Field.ANAL_Q4.ToString(), objEntr.ANAL_Q4);
            AddParameter(QuestionInfo.Field.ANAL_Q5.ToString(), objEntr.ANAL_Q5);
            AddParameter(QuestionInfo.Field.ANAL_Q6.ToString(), objEntr.ANAL_Q6);
            AddParameter(QuestionInfo.Field.ANAL_Q7.ToString(), objEntr.ANAL_Q7);
            AddParameter(QuestionInfo.Field.ANAL_Q8.ToString(), objEntr.ANAL_Q8);
            AddParameter(QuestionInfo.Field.ANAL_Q9.ToString(), objEntr.ANAL_Q9);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        Int64 ID
        )
        {
            connect();
            InitSPCommand(_strSPDeleteName);
            AddParameter(QuestionInfo.Field.ID.ToString(), ID);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public DataTableCollection Get_Page(QuestionInfo obj, string orderBy, int pageIndex, int pageSize, ref String sErr)
        {
            string whereClause = CreateWhereClause(obj);
            DataTableCollection dtList = null;
            connect();
            InitSPCommand(_strSPGetPages);

            AddParameter("WhereClause", whereClause);
            AddParameter("OrderBy", orderBy);
            AddParameter("PageIndex", pageIndex);
            AddParameter("PageSize", pageSize);

            try
            {
                dtList = executeCollectSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return dtList;
        }

        public Boolean IsExist(
        Int64 ID
        )
        {
            connect();
            InitSPCommand(_strSPIsExist);
            AddParameter(QuestionInfo.Field.ID.ToString(), ID);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(QuestionInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            connect();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method


        public DataTable GetByANALName(string cate, ref string sErr)
        {
            string query = "select ANAL_" + cate + " as CODE from " + _strTableName + " Group by  ANAL_" + cate;
            DataTable list = new DataTable();
            connect();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }



        public int GetCountANAL_LV(string cate, string code, int difF, int difT, ref string sErr)
        {
            int ret = -1;
            string query = "select Count(*) from " + _strTableName + " WHERE ANAL_" + cate + " =  N'" + code + "' AND (Difficulty between " + difF + " and " + difT + ") and Enabled = 1";
            DataTable list = new DataTable();
            connect();
            try
            {
                object tmp = executeScalar(query);
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return ret;
        }

        public DataTable GetEnableAll(bool val, ref string sErr)
        {
            QuestionInfo objEntr = new QuestionInfo();
            connect();
            InitSPCommand(_strSPGetEnableName);
            AddParameter(QuestionInfo.Field.Enabled.ToString(), val);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();

            //if (list.Rows.Count > 0)
            //    objEntr = (QuestionInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
    }
}
